package com.cykj.service.impl;

import com.cykj.dto.ResponseDTO;
import com.cykj.entity.CyGoodDO;
import com.cykj.mapper.GoodMapper;
import com.cykj.service.IGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Project:JDMS
 * Package:com.cykj.service.impl
 *
 * @Description:
 * @Author:其一
 * @CreateTime: 2025-07-30-10-24
 */
@Service
public class GoodServiceImpl implements IGoodService {

    @Autowired
    private GoodMapper goodMapper;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public ResponseDTO getAllGood() {
        List<CyGoodDO> allGood = goodMapper.getAllGood();
        for (CyGoodDO cyGoodDO : allGood) {
            redisTemplate.opsForValue().set("good:"+cyGoodDO.getGoodId(),cyGoodDO);
            redisTemplate.opsForValue().set("goodStock:"+cyGoodDO.getGoodId(),cyGoodDO.getGoodStock());
        }
        return ResponseDTO.success("查询成功");
    }

    @Override
    public ResponseDTO updateGoodStock(Integer count, Integer goodId) {
        Integer res = goodMapper.updateGoodStock(count, goodId);
        if(res == 0 || res ==null){
            return ResponseDTO.error("库存扣除失败");
        }
        return ResponseDTO.success("库存扣除成功");
    }
}
